import React, { useState, useEffect, useCallback } from 'react'
import { message, Table, Button, Space, Row, Col, Popconfirm } from 'antd'
import { DeleteOutlined, FormOutlined } from '@ant-design/icons'
import { useNavigate, useSearchParams } from 'react-router-dom'
import request, { baseUrl } from '@/utils/request'
import { useSelector } from 'react-redux'

const List = () => {
    const [urlParams, setUrlParams] = useSearchParams()
    const [params, setParams] = useState({
        // 解决数据类型为string引起的bug
        page: Number(urlParams.get('page')) || 1,
        size: Number(urlParams.get('size')) || 10
    })
    const [total, setTotal] = useState(0)
    const navigate = useNavigate();


    const [datalist, setDatalist] = useState([]);
    let controller
    const getData = async () => {
        controller = new AbortController()
        const { data } = await request.get('/user/selAlluser', {
            params,
            signal: controller.signal
        })

        setDatalist(data.data)
        setTotal(data.total)
    }

    const userInfo = useSelector((state) => state.userInfo)

    const removeItem = useCallback(async (id) => {
        const { data } = await request.post('/user/deluser', { uid: id })
        if (data.code === 200) {
            message.success('用户删除成功')
            getData()
        }
    }, [datalist])
    
    const [selectIds, setSelectIds] = useState([])
    const removeItems = useCallback(async () => {
        if (selectIds.length === 0) {
            return message.warning('请选择删除的用户');
        }
        const { data } = await request.post('/user/delUsers', {
            ids: selectIds
        })

        if (data.code === 200) {
            message.success('用户删除成功')
            getData()
        }
    }, [selectIds])

    const editItem = useCallback((id) => navigate('/manage/user/' + id), [])

    const columns = [
        {
            title: '#',
            key: 'uid',
            width: 10,
            render(text, record, index) {
                return index + 1
            }
        },
        {
            title: '用户名',
            dataIndex: 'username',
            width: 120,
            render: (text) => {
                return <h4>{text}</h4>
            }
        },
        {
            title: '手机号码',
            dataIndex: 'telphone',
            width: 120,
            render: (text) => {
                return <h4>{text}</h4>
            }
        },
        {
            title: '用户密码',
            dataIndex: 'password',
            width: 200,
            render: (text) => {
                return <h4>{text}</h4>
            }
        },
        {
            title: '操作',
            dataIndex: 'uid',
            width: 100,
            render(uid) {
                return (
                    // <React.Fragment>
                    <Space>
                        <Button ghost type='primary' icon={<FormOutlined />} size="small" onClick={editItem.bind(null, uid)}></Button>
                        <Popconfirm
                            title="确认删除"
                            onConfirm={removeItem.bind(null, uid)}
                        >
                            <Button danger icon={<DeleteOutlined />} size="small"></Button>

                        </Popconfirm>
                    </Space>
                )
            }
        },
    ]
    const rowSelection = {
        onChange(selectedRowKeys) {
            console.log('selectedRowKeys=', selectedRowKeys)
            setSelectIds(selectedRowKeys)
        }
    }
    const pagination = {
        total,
        current: params.page,
        pageSize: params.size,
        size: 'small',
        onChange(page, size) {
            setParams({
                page,
                size
            })

            setUrlParams({
                page,
                size
            })
        }
    }


    useEffect(() => {
        getData();

        return function () {
            // 取消ajax
            controller.abort()
        }
    }, [params])



    return (
        <div>
            <Row gutter={20}>

                <Col span={24} className='text-right'>
                    <Space>

                        <Popconfirm
                            title="确认删除"
                            onConfirm={removeItems}
                        >
                            <Button danger icon={<DeleteOutlined />}>批量删除</Button>

                        </Popconfirm>

                    </Space>
                </Col>
            </Row>
            <Table
                style={{ marginTop: 20 }}
                rowKey="uid"
                rowSelection={rowSelection}
                columns={columns}
                dataSource={datalist}
                pagination={pagination}
            />
        </div>

    )
}

export default List;